#ifndef WIKI_H
#define WIKI_H

#include <QListWidgetItem>
#include <QString>
#include <QStringList>
#include <QFlags>

class QWebEngineView;

class Wiki {
public:
    enum SearchMode {
        Text       = 0x01,   // 走 searching      & content
        ListItem   = 0x02,   // 走 searching_item & content_item
    };

    enum ContentMode {
        Url          = 0x01,
        Content      = 0x02,
        UrlOrContent = 0x04,

        NoContent = 100, // 自行操作 QWebEngineView
    };

    virtual QString name() = 0;
    virtual QString prefix() { return "";};
    virtual bool autoLoad() { return false; };
    virtual bool autoLoadFirst() { return false; }

    virtual int searchDelay() { return 500; };
    virtual SearchMode searchMode() { return Text; }
    virtual QStringList searching(const QString &text) { return QStringList(); };
    virtual QList<QListWidgetItem*> searching_item(const QString &text) { return QList<QListWidgetItem*>();};

    virtual ContentMode contentMode() { return Content; };
    virtual QString content(const QString &text) { return QString(); };
    virtual QString content_item(const QListWidgetItem *item) { return "";};

    // By NoContent
    virtual void content(const QString &text, QWebEngineView *view) {  };
    virtual void content_item(const QListWidgetItem *item, QWebEngineView *view) { };


    /**
     * @brief 转到内容域
     * @note  仅限 Text 搜索
     * @return
     */
    virtual bool forward_to_content() { return false; }

    /**
     * @brief 交互式域
     * @note  新特性
     * @return
     */
    virtual bool interactive(const QString &text) { return false; }
    virtual bool interactive_running() { return false; } ;
    virtual void interactive_restore() {  };


    virtual bool once_reload() {
        auto do1 = m_once_reload;
        m_once_reload = false;
        return do1;
    }

protected:
    QListWidgetItem* newItem() {return new QListWidgetItem;}
    int m_interactive;
    int m_once_reload;
};


#define WIKI_AUTOLOAD                       \
    bool autoLoad() override {              \
        return true;                        \
    }
#define WIKI_AUTOLOAD_FIRST                 \
    bool autoLoadFirst() override {         \
            return true;                    \
    }
#define WIKI_USE_DELAY(x)                   \
    int searchDelay() override {            \
        return x;                           \
    }
#define WIKI_USE_TEXT                       \
    SearchMode searchMode() override {      \
        return Text;                        \
    }
#define WIKI_USE_LIST                       \
    SearchMode searchMode() override {      \
        return ListItem;                    \
    }
#define WIKI_USE_CONTENT                    \
    ContentMode contentMode() override {    \
        return Content;                     \
    }
#define WIKI_USE_URL                        \
    ContentMode contentMode() override {    \
        return Url;                         \
    }
#define WIKI_USE_URL_CONTENT                \
    ContentMode contentMode() override {    \
        return UrlOrContent;                \
    }
#define WIKI_NO_CONTENT                     \
    ContentMode contentMode() override {    \
        return NoContent;                   \
    }
#define WIKI_FORWARED_CONTENT               \
    bool forward_to_content() override { return true; }
#endif // WIKI_H
